<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version

  NOTES:
  jalali_to_gregorian() and gregorian_to_jalali() are
  Copyright (C) 2000 under GNU GPL by Roozbeh Pournader and Mohammad Toossi
  http://iranphp.net/
  Sallar Kaboli hooks his copyright to it in 2004 which is incorrect
  They are slightly modified for a better ease of use

  Most countries that observe daylight saving time are listed below.
  They all save one hour in the summer and change their clocks some time
  between midnight and 3 am.
  Data taken from http://webexhibits.org/daylightsaving/g.html

  the integer containing the country data is the country int. dialcode since
  ISO 3166 isn't detailed enough unless you have the money for ISO 3166-3.
  http://kropla.com/dialcode.htm
**********************************************/

/*
  Notes:
  PHP date, strftime, gmdate, gmstrftime and gmmktime functions are
  DST sensitive
  Without a given time:
    - date() uses the current server time.
      This is DST affected if the server uses DST
    - gmdate() uses the current GMT time, it extracts server GMT & DST.
  With a given time
    * if server time is DST but the given time isn't then DST is extracted
    * if server time isn't DST but the given time is then DST is added
    - date() add/extracts DST
    - gmdate() extracts server GMT & DST
*/

abstract class L10NTime
{

	# Convert the GMT time to local time
	public static function tolocal($time)
	{
		trigger_deprecated();
		return $time + date('Z',$time);
	}

	# Convert 01-01-2005 12:00:00 GMT to 01-01-2005 12:00:00 Local Time
	# Convert the local time to GMT
	public static function toGMT($time)
	{
		trigger_deprecated();
		return $time - date('Z',$time);
	}

	public static function strftime($format, $time=null)
	{
		$strftime2date = array(
			// Day
			'%a' => 'D',
			'%A' => 'l',
			'%d' => 'd',
			'%e' => 'j',
			'%j' => 'z',
			'%u' => 'N',
			'%w' => 'w',
			// Week
			'%U' => 'W',
			'%V' => 'W',
			'%W' => 'W',
			// Month
			'%b' => 'M',
			'%B' => 'F',
			'%h' => 'M',
			'%m' => 'm',
			// Year
			'%C' => 'o',
			'%g' => 'o',
			'%G' => 'o',
			'%y' => 'y',
			'%Y' => 'Y',
			// Time
			'%H' => 'H',
			'%k' => 'G',
			'%I' => 'h',
			'%l' => 'g',
			'%M' => 'i',
			'%p' => 'A',
			'%P' => 'a',
			'%r' => 'h:i:s A',
			'%R' => 'H:i',
			'%S' => 's',
			'%T' => 'H:i:s',
			'%X' => 'H:i:s',
			'%z' => 'O',
			'%Z' => 'e',
			// Time and Date Stamps
			'%c' => 'DATE_L',
			'%D' => 'm/d/y',
			'%F' => 'Y-m-d',
			'%s' => 'U',
			'%x' => 'm-d-Y',
			// Miscellaneous
			'%n' => "\n",
			'%t' => "\t",
			'%%' => '%',
		);
		$format = str_replace(array_keys($strftime2date), array_values($strftime2date), $format);
		trigger_deprecated("use Dragonfly::getKernel()->L10N->date('{$format}', {$time})");

		# return the formatted string
		return Dragonfly::getKernel()->L10N->date($format, $time);
	}

	public static function date($format, $time=null)
	{
		return Dragonfly::getKernel()->L10N->date($format, $time);
	}

	public static function strtotime($time, $now=0)
	{
		trigger_deprecated();
		return strtotime($time, $now<1?time():$now);
	}

	# Check if the local time is inside DST depending on region
	public static function in_dst($localtime)
	{
		trigger_deprecated();
		return 0 < date('I',$localtime);
	}

	# Convert the local time to DST depending on region
	public static function get_dst_time($localtime)
	{
		trigger_deprecated();
		return $localtime;
	}
/*
  Start internal functions mostly not used outside this class
*/
	public static function get_dst_switch($localtime, $data)
	{
		trigger_deprecated();
		return 0;
	}

	public static function is_dst($localtime, $start, $end)
	{
		trigger_deprecated();
		return 0 < date('I',$localtime);
	}

	public static function gregorian_to_jalali($g_y, $g_m, $g_d)
	{
		$g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		$j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);
		$gy = $g_y-1600;
		$gm = $g_m-1;
		$gd = $g_d-1;
		$g_day_no = 365*$gy + intval(($gy+3)/4) - intval(($gy+99)/100) + intval(($gy+399)/400);
		for ($i=0; $i < $gm; ++$i)
			$g_day_no += $g_days_in_month[$i];
		/* leap and after Feb */
		if ($gm>1 && (($gy%4==0 && $gy%100!=0) || ($gy%400==0)))
			++$g_day_no;
		$g_day_no += $gd;
		$j_day_no = $g_day_no-79;
		$j_np = intval($j_day_no/12053);
		$j_day_no %= 12053;
		$jy = 979+33*$j_np+4*intval($j_day_no/1461);
		$j_day_no %= 1461;
		if ($j_day_no >= 366) {
			$jy += intval(($j_day_no-1)/365);
			$j_day_no = ($j_day_no-1)%365;
		}
		for ($i = 0; $i < 11 && $j_day_no >= $j_days_in_month[$i]; ++$i) {
			$j_day_no -= $j_days_in_month[$i];
		}
		$jm = $i+1;
		$jd = $j_day_no+1;
		return array($jy, $jm, $jd);
	}

	public static function jalali_to_gregorian($j_y, $j_m, $j_d)
	{
		$g_days_in_month = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		$j_days_in_month = array(31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);
		$jy = $j_y-979;
		$jm = $j_m-1;
		$jd = $j_d-1;
		$j_day_no = 365*$jy + intval($jy/33)*8 + intval(($jy%33+3)/4);
		for ($i=0; $i < $jm; ++$i)
			$j_day_no += $j_days_in_month[$i];
		$j_day_no += $jd;
		$g_day_no = $j_day_no+79;
		$gy = 1600 + 400*intval($g_day_no/146097);
		$g_day_no = $g_day_no % 146097;
		$leap = true;
		if ($g_day_no >= 36525) {
			$g_day_no--;
			$gy += 100*intval($g_day_no/36524);
			$g_day_no = $g_day_no % 36524;
			if ($g_day_no >= 365)
				$g_day_no++;
			else
				$leap = false;
		}
		$gy += 4*intval($g_day_no/1461);
		$g_day_no %= 1461;
		if ($g_day_no >= 366) {
			$leap = false;
			$g_day_no--;
			$gy += intval($g_day_no/365);
			$g_day_no = $g_day_no % 365;
		}
		for ($i = 0; $g_day_no >= $g_days_in_month[$i] + ($i == 1 && $leap); $i++) {
			$g_day_no -= $g_days_in_month[$i] + ($i == 1 && $leap);
		}
		$gm = $i+1;
		$gd = $g_day_no+1;
		return array($gy, $gm, $gd);
	}

}
